import { createBrowserRouter, Navigate } from 'react-router-dom';
import Login from '@/pages/Login';
import Layout from '@/layouts/BasicLayout';
import Dashboard from '@/pages/Dashboard';
import UserManagement from '@/pages/User';
import RoleManagement from '@/pages/Role';
import PermissionManagement from '@/pages/Permission';
import MenuManagement from '@/pages/Menu';
import AuditLogManagement from '@/pages/AuditLog';
import LoginLogManagement from '@/pages/LoginLog';
import FileManagement from '@/pages/File';
import TenantManagement from '@/pages/Tenant';
import PlanManagement from '@/pages/Plan';
import DictionaryManagement from '@/pages/Dictionary';
import FileConfigManagement from '@/pages/FileConfig';
import CompanyManagement from '@/pages/Company';
import DepartmentManagement from '@/pages/Department';
import TableConfigManagement from '@/pages/TableConfig';
import Profile from '@/pages/Profile';

// 路由守卫组件
const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
  // 从 localStorage 读取 token（与 axios 保持一致）
  const accessToken = localStorage.getItem('access_token');
  
  if (!accessToken) {
    return <Navigate to="/login" replace />;
  }
  
  return <>{children}</>;
};

// 登录页守卫
const LoginRoute = ({ children }: { children: React.ReactNode }) => {
  // 从 localStorage 读取 token（与 axios 保持一致）
  const accessToken = localStorage.getItem('access_token');
  
  if (accessToken) {
    return <Navigate to="/" replace />;
  }
  
  return <>{children}</>;
};

const router = createBrowserRouter([
  {
    path: '/login',
    element: (
      <LoginRoute>
        <Login />
      </LoginRoute>
    ),
  },
  {
    path: '/',
    element: (
      <ProtectedRoute>
        <Layout />
      </ProtectedRoute>
    ),
    children: [
      {
        index: true,
        element: <Dashboard />,
      },
      // 系统管理
      {
        path: 'system/user',
        element: <UserManagement />,
      },
      {
        path: 'system/role',
        element: <RoleManagement />,
      },
      {
        path: 'system/permission',
        element: <PermissionManagement />,
      },
      {
        path: 'system/menu',
        element: <MenuManagement />,
      },
      {
        path: 'system/audit-log',
        element: <AuditLogManagement />,
      },
      {
        path: 'system/dictionary',
        element: <DictionaryManagement />,
      },
      {
        path: 'system/company',
        element: <CompanyManagement />,
      },
      {
        path: 'system/department',
        element: <DepartmentManagement />,
      },
      // 日志管理
      {
        path: 'logs/login',
        element: <LoginLogManagement />,
      },
      {
        path: 'logs/audit',
        element: <AuditLogManagement />,
      },
      // 文件管理
      {
        path: 'files/list',
        element: <FileManagement />,
      },
      {
        path: 'files/config',
        element: <FileConfigManagement />,
      },
      // 租户管理
      {
        path: 'tenant/list',
        element: <TenantManagement />,
      },
      {
        path: 'tenant/plan',
        element: <PlanManagement />,
      },
      // 系统配置
      {
        path: 'config/table',
        element: <TableConfigManagement />,
      },
      // 个人中心
      {
        path: 'profile',
        element: <Profile />,
      },
    ],
  },
]);

export default router;

